<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 会议室预订系统</title>
    <!-- 防止浏览器缓存页面 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 重置默认样式并设置全局字体 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        /* 页面背景设置为渐变色，并居中显示容器 */
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        /* 主容器样式：毛玻璃效果、圆角、阴影 */
        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 400px;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }

        /* 容器装饰效果：对角线渐变背景 */
        .container::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            z-index: 0;
        }

        /* 页眉样式设置 */
        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
            z-index: 1;
        }

        /* 标题样式：渐变文字效果 */
        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        /* 副标题样式 */
        .header p {
            color: #666;
            font-size: 14px;
        }

        /* 表单组样式 */
        .form-group {
            margin-bottom: 25px;
            position: relative;
            z-index: 1;
        }

        /* 表单标签样式 */
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
            font-size: 14px;
        }

        /* 输入框包装器 */
        .input-with-icon {
            position: relative;
        }

        /* 输入框图标样式 */
        .input-with-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #777;
        }

        /* 输入框样式 */
        .form-group input {
            width: 100%;
            padding: 15px 15px 15px 45px;
            border: 2px solid #e1e1e1;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        /* 输入框焦点样式 */
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        /* 错误信息样式 */
        .error-message {
            color: #e74c3c;
            background-color: rgba(231, 76, 60, 0.1);
            border: 1px solid #e74c3c;
            border-radius: 8px;
            padding: 12px;
            margin: 15px 0;
            display: none;
            font-size: 14px;
            text-align: center;
        }

        /* 登录按钮样式 */
        #loginButton {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            z-index: 1;
        }

        /* 登录按钮悬停效果 */
        #loginButton:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
        }

        /* 登录按钮按下效果 */
        #loginButton:active {
            transform: translateY(0);
        }

        /* 登录按钮禁用状态 */
        #loginButton:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        /* 按钮加载动画 */
        .loading-spinner {
            display: none;
        }

        .loading-spinner i {
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* 注册链接样式 */
        .register-link {
            margin-top: 20px;
            color: #666;
            text-align: center;
            position: relative;
            z-index: 1;
        }

        /* 链接样式 */
        .register-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }
        
        /* 链接悬停效果 */
        .register-link a:hover {
            text-decoration: underline;
        }
        
        /* 页脚样式 */
        .footer {
            text-align: center;
            margin-top: 30px;
            color: #777;
            font-size: 12px;
            position: relative;
            z-index: 1;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .container {
                padding: 30px 20px;
            }

            .header h1 {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <!-- 登录表单容器 -->
    <div class="container">
        <div class="header">
            <h1><i class="fas fa-calendar-alt"></i> 会议室预订系统</h1>
            <p>请输入您的账户信息登录</p>
        </div>

        <!-- 登录表单 -->
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-with-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" id="username" name="username" placeholder="请输入用户名" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-with-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="password" name="password" placeholder="请输入密码" required autocomplete="current-password">
                </div>
            </div>
            
            <!-- 错误信息显示区域 -->
            <div id="errorMessage" class="error-message" role="alert" aria-live="assertive"></div>
            
            <!-- 登录按钮 -->
            <button type="submit" id="loginButton">
                <span class="button-text">登录</span>
                <span class="loading-spinner"><i class="fas fa-spinner"></i> 登录中...</span>
            </button>
        </form>

        <div class="register-link">
            <p>如果您没有账户，请<a href="/api/register.html">注册</a></p>
        </div>

        <div class="footer">
            <p>© 2025 会议室预订系统. 保留所有权利.</p>
        </div>
    </div>
    
    <script>
        // 表单提交事件处理
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            // 阻止表单默认提交行为
            e.preventDefault();
            
            // 获取用户名和密码
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('errorMessage');
            const loginButton = document.getElementById('loginButton');
            const buttonText = loginButton.querySelector('.button-text');
            const loadingSpinner = loginButton.querySelector('.loading-spinner');
            
            // 隐藏之前的错误信息
            errorMessage.style.display = 'none';
            
            // 设置按钮为加载状态
            loginButton.disabled = true;
            buttonText.style.display = 'none';
            loadingSpinner.style.display = 'block';
            
            // 发送登录请求到后端
            fetch('/api/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
            })
            .then(response => response.json())
            .then(data => {
                // 根据后端返回结果处理登录成功或失败
                if (data.success) {
                    // 登录成功，将用户名存储到localStorage
                    localStorage.setItem('username', username);
                    // 跳转到主页
                    window.location.href = '/api/major.html';
                } else {
                    // 显示错误信息
                    errorMessage.textContent = data.message;
                    errorMessage.style.display = 'block';
                }
            })
            .catch(error => {
                // 网络错误处理
                errorMessage.textContent = '登录请求失败，请稍后重试';
                errorMessage.style.display = 'block';
            })
            .finally(() => {
                // 恢复按钮状态
                loginButton.disabled = false;
                buttonText.style.display = 'block';
                loadingSpinner.style.display = 'none';
            });
        });
        
        // 回车键登录功能 - 使用与表单提交相同的逻辑
        document.addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                // 防止默认回车行为（如文本域换行）
                event.preventDefault();
                
                // 触发表单提交事件
                document.getElementById('loginForm').dispatchEvent(new Event('submit'));
            }
        });
    </script>
</body>
</html>